package com.nmgsr.ptmsapp.activity;

import android.app.DatePickerDialog;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.nmgsr.beans.requestdao.PageReq;
import com.nmgsr.beans.responsedao.PtmsVideoCaptureRes;
import com.nmgsr.beans.responsedao.PtmsWarnTestRes;
import com.nmgsr.beans.service.Api;
import com.nmgsr.nettools.base.BaseActivity;
import com.nmgsr.nettools.net.config.NetManager;
import com.nmgsr.nettools.net.retrofit.BaseResponse;
import com.nmgsr.nettools.net.retrofit.HttpHelper;
import com.nmgsr.nettools.net.retrofit.HttpResponseObserver;
import com.nmgsr.nettools.net.retrofit.HttpUtil;
import com.nmgsr.nettools.net.retrofit.PagedResult;
import com.nmgsr.ptmsapp.R;
import com.nmgsr.ptmsapp.adapter.ListPtmsVideoCaptureAdapter;
import com.nmgsr.ptmsapp.adapter.ListPtmsWarnTestAdapter;
import com.nmgsr.ptmsapp.config.PreferencesManager;
import com.nmgsr.ptmsapp.view.MyDividerItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ListPtmsVideoCaptureActivity extends BaseActivity {
    private static final String TAG = ListPtmsVideoCaptureActivity.class.getSimpleName();
    private EditText et_search;
    private SmartRefreshLayout refreshLayout;

    private ListPtmsVideoCaptureAdapter adapter;
    private int page = 1;
    private String token;
    private List<String> photoList = new ArrayList<>();
    private ImageView iv_date;
    private List<PtmsVideoCaptureRes> roleList;
    @Override
    protected int getLayoutId() {
        return R.layout.list_ptms_video_capture_activity;
    }
    @Override
    protected void initView() {
        super.initView();
        mContext = ListPtmsVideoCaptureActivity.this;
        fl_actionbar.setVisibility(View.GONE);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        token = PreferencesManager.getToken(mContext);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));

        MyDividerItemDecoration divider = new MyDividerItemDecoration(mContext, MyDividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.custom_divider));
        recyclerView.addItemDecoration(divider);

        adapter = new ListPtmsVideoCaptureAdapter();
        recyclerView.setAdapter(adapter);

        refreshLayout = findViewById(R.id.refresh_layout);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            getData();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            getData();
        });
        //adapter点击事件
        adapter.setOnItemClickListener((adapter, view, position) -> {
            photoList.clear();
            //ImageActivity.startActivity(mContext, (ArrayList<String>) photoList, 0,false);
            PtmsVideoCaptureRes ptmsVideoCaptureRes =roleList.get(position);
            photoList.add(ptmsVideoCaptureRes.getCapturePath());
            ImageActivity.startActivity(mContext, (ArrayList<String>) photoList, 0,false);

        });
        iv_date = findViewById(R.id.iv_date);
        iv_date.setOnClickListener(v -> {
            showDatePicker(new DatePickCallback() {
                @Override
                public void onDateSet(String startDate, String endDate) {
                    Log.e(TAG, "startDate: " + startDate + "  endDate: " + endDate);
                }
            });
        });

    }

    protected void initData() {
        refreshLayout.autoRefresh();

    }

    protected void onResume() {
        super.onResume();
        initData();
    }

    private void getData() {
        PageReq pageReq = new PageReq();
        pageReq.setPage(page);
        pageReq.setPageSize(20);
        NetManager.getInstance().setAuthorizationToken(token);
        HttpHelper.getInstance()
                .getService(Api.class)
                .getPtmsVideoCaptureList(pageReq)
                .compose(HttpUtil.defaultSchedulers())
                .compose(bindToLifecycle())
                .subscribe(new HttpResponseObserver<BaseResponse<PagedResult<PtmsVideoCaptureRes>>>() {

                    @Override
                    protected void onSuccess(BaseResponse<PagedResult<PtmsVideoCaptureRes>> response) {
                        Log.e(TAG, "PtmsToolResponse: " + new Gson().toJson(response));
                        if (response.isSuccess()) {
                            roleList = response.getResult().getItems();
                            if (page == 1) {
                                if (roleList != null && !roleList.isEmpty()) {
                                    // 更新UI数据
                                    adapter.setList(roleList);
                                    refreshLayout.finishRefresh();
                                } else {
                                    QMUIEmptyView qmuiEmptyView = new QMUIEmptyView(mContext);
                                    qmuiEmptyView.show(false, "暂无数据", null, getString(R.string.emptyView_mode_desc_retry),
                                            v -> refreshLayout.autoRefresh());
                                    adapter.setEmptyView(qmuiEmptyView);
                                    refreshLayout.finishRefresh();
                                }
                            } else {
                                adapter.addData(roleList);
                                refreshLayout.finishLoadMore();
                            }
                        } else {
                            Log.e(TAG, "业务错误：" + response.getMessage());
                            Toast.makeText(mContext, "错误：" + response.getMessage(), Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh(false);
                        }
                    }

                    @Override
                    protected void handleError(Throwable e) {
                        Log.e(TAG, "网络异常：", e);
                    }
                });
    }
    public interface DatePickCallback {
        void onDateSet(String startDate, String endDate);
    }
    private void showDatePicker(DatePickCallback callback) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // 处理选择的日期
                String date = year + "-" + (month + 1) + "-" + dayOfMonth;
                String endDate = year + "-" + (month + 1) + "-" + (dayOfMonth + 1); // 天数加一
                callback.onDateSet(date, endDate); // 默认设置上传选择的日期一天以内
//                ToastUtil.showToast("你选择的日期是：" + date);
            }
        }, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));

        // 设置最小日期为今天
//        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        // 设置最大日期为未来一个月
//        Calendar maxCalendar = Calendar.getInstance();
//        maxCalendar.add(Calendar.MONTH, 1);
//        datePickerDialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis());
        // 设置最大日期为今天
        datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() - 1000);

        datePickerDialog.show();
    }
}
